package com.hmjk.health.util;

public class BP_Evaluator {
	public static final int TYPE_LOW = 0;
	public static final int TYPE_NORMAL = 1;
	public static final int TYPE_NORMALHIGH = 2;
	public static final int TYPE_1HIGH = 3;
	public static final int TYPE_2HIGH = 4;
	public static final int TYPE_3HIGH = 5;
	
	public static final int LEV_HIGH = 2;
	public static final int LEV_NORMAL = 1;
	public static final int LEV_LOW = 0;
	
	private static BP_Evaluator me;
	private BPRANGE SBP_LOW;
	private BPRANGE DBP_LOW;
	
	private BPRANGE SBP_NORMAL;
	private BPRANGE DBP_NORMAL;
	
	private BPRANGE SBP_NHIGH;
	private BPRANGE DBP_NHIGH;
	
	private BPRANGE SBP_1HIGH;
	private BPRANGE DBP_1HIGH;
	
	private BPRANGE SBP_2HIGH;
	private BPRANGE DBP_2HIGH;
	
	private BPRANGE SBP_3HIGH;
	private BPRANGE DBP_3HIGH;
	
	public static BP_Evaluator getInstance(){
		if(me == null)
			me = new BP_Evaluator();
		return me;
	}
	
	private BP_Evaluator(){
		SBP_LOW = new BPRANGE(0,89);
		DBP_LOW = new BPRANGE(0,59);
		
		SBP_NORMAL = new BPRANGE(90,119);
		DBP_NORMAL = new BPRANGE(60,79);
		
		SBP_NHIGH = new BPRANGE(120,139);
		DBP_NHIGH = new BPRANGE(80,89);
		
		SBP_1HIGH = new BPRANGE(140,159);
		DBP_1HIGH = new BPRANGE(90,99);
		
		SBP_2HIGH = new BPRANGE(160,179);
		DBP_2HIGH = new BPRANGE(100,109);
		
		SBP_3HIGH = new BPRANGE(180,999);
		DBP_3HIGH = new BPRANGE(110,999);
	}
	
	class BPRANGE{
		public int upper;
		public int lower;
		
		BPRANGE(int low, int up){
			upper = up;
			lower = low;
		}
		
		public boolean in(int value){
			return ((value >= lower) && (value <= upper));
		}
		
		public int level(int value){
			return ((value - lower) * 10 /(upper - lower + 1));
		}
	}
	
	public class Result{
		public int type = TYPE_NORMAL;
		public int subLevel = 0;//0..9
	}
	
	private Result getResult(int t, int l){
		Result r = new Result();
		r.type = t;
		r.subLevel = l;
		return r;
	}
	
	public static Result evaluateLevel(int sbp, int dbp){
		BP_Evaluator ev = getInstance();
		
		if(ev.SBP_3HIGH.in(sbp) || ev.DBP_3HIGH.in(dbp)){
			//3high
			int l = 0;
			if(ev.SBP_3HIGH.in(sbp)){
				l = ev.SBP_3HIGH.level(sbp);
			}
			
			if(ev.DBP_3HIGH.in(dbp)){
				l = Math.max(l,ev.DBP_3HIGH.level(dbp));
			}
			
			return ev.getResult(TYPE_3HIGH, l);
		}
		
		if(ev.SBP_2HIGH.in(sbp) || ev.DBP_2HIGH.in(dbp)){
			//2high
			int l = 0;
			if(ev.SBP_2HIGH.in(sbp)){
				l = ev.SBP_2HIGH.level(sbp);
			}
			
			if(ev.DBP_2HIGH.in(dbp)){
				l = Math.max(l,ev.DBP_2HIGH.level(dbp));
			}
			
			return ev.getResult(TYPE_2HIGH, l);
		}
		
		if(ev.SBP_1HIGH.in(sbp) || ev.DBP_1HIGH.in(dbp)){
			//1high
			int l = 0;
			if(ev.SBP_1HIGH.in(sbp)){
				l = ev.SBP_1HIGH.level(sbp);
			}
			
			if(ev.DBP_1HIGH.in(dbp)){
				l = Math.max(l,ev.DBP_1HIGH.level(dbp));
			}
			
			return ev.getResult(TYPE_1HIGH, l);
		}
		
		if(ev.SBP_NHIGH.in(sbp) || ev.DBP_NHIGH.in(dbp)){
			//normal high
			int l = 0;
			if(ev.SBP_NHIGH.in(sbp)){
				l = ev.SBP_NHIGH.level(sbp);
			}
			
			if(ev.DBP_NHIGH.in(dbp)){
				l = Math.max(l,ev.DBP_NHIGH.level(dbp));
			}
			
			return ev.getResult(TYPE_NORMALHIGH, l);
		}
		
		if(ev.SBP_NORMAL.in(sbp) || ev.DBP_NORMAL.in(dbp)){
			//normal high
			int l = 0;
			if(ev.SBP_NORMAL.in(sbp)){
				l = ev.SBP_NORMAL.level(sbp);
			}
			
			if(ev.DBP_NORMAL.in(dbp)){
				l = Math.max(l,ev.DBP_NORMAL.level(dbp));
			}
			
			return ev.getResult(TYPE_NORMAL, l);
		}
		
		int l = 0;
		l = ev.SBP_LOW.level(sbp);
		l = Math.min(l, ev.DBP_LOW.level(dbp));
		return ev.getResult(TYPE_LOW, l);
	}
	
	public static int checkSBP(int sbp){
		BP_Evaluator ev = getInstance();
		
		if(sbp > ev.SBP_NHIGH.upper)
			return LEV_HIGH;
		else if(sbp < ev.SBP_NORMAL.lower)
			return LEV_LOW;
		
		return LEV_NORMAL;
	}
	
	public static int checkDBP(int dbp){
		BP_Evaluator ev = getInstance();
		
		if(dbp > ev.DBP_NHIGH.upper)
			return LEV_HIGH;
		else if(dbp < ev.DBP_NORMAL.lower)
			return LEV_LOW;
		
		return LEV_NORMAL;
	}
}
